package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.back.domain.UserCollect
import com.ruoyi.back.mapper.UserCollectMapper
import com.ruoyi.back.service.UserCollectService
import org.springframework.stereotype.Service

@Service
class UserCollectServiceImpl : ServiceImpl<UserCollectMapper, UserCollect>(), UserCollectService {

    override fun isLike(userId: Long, templateId: Long): Boolean =
        getOne(
            KtQueryWrapper(UserCollect::class.java).eq(UserCollect::type, 1)
                .eq(UserCollect::createBy, userId).eq(UserCollect::templateId, templateId)
        )?.run { true } ?: false

    override fun templateCount(templateId: Long): Pair<Int, Int> {

       return (count(
            KtQueryWrapper(UserCollect::class.java)
                .eq(UserCollect::templateId, templateId)
                .eq(UserCollect::type, 1)
        )) to (
                count(
                    KtQueryWrapper(UserCollect::class.java)
                        .eq(UserCollect::templateId, templateId)
                        .eq(UserCollect::type, 2)
                )
                )
    }

    override fun isCollect(userId: Long, templateId: Long): Boolean =
        getOne(
            KtQueryWrapper(UserCollect::class.java).eq(UserCollect::type, 2)
                .eq(UserCollect::createBy, userId).eq(UserCollect::templateId, templateId)
        )?.run { true } ?: false
}